package com.example.com.daervideo.popuwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.com.daervideo.R;
import com.example.com.daervideo.entity.BaseListEntity;
import com.example.com.daervideo.initerface.ApiService;
import com.example.com.daervideo.utils.RetrofitClient;

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Created by i7-1 on 2018/12/10.
 */

public class ComplainPpwiondow extends PopupWindow {

    private final View mMenuView;
    private final Context context;
    private final ApiService apiService;
    private List lists = new ArrayList();
    private RecyclerView recyclerView;
    private List selectList = new ArrayList<>();

    public ComplainPpwiondow(Context context) {
        this.context = context;
        mMenuView = LayoutInflater.from(context).inflate(R.layout.popuwindow_complain, null);
        apiService = RetrofitClient.getInstance().create(ApiService.class);
        mMenuView.findViewById(R.id.out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mMenuView.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                complain();
            }
        });
        initRecycleView();
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x30000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

    }


    private void initRecycleView() {
        recyclerView = mMenuView.findViewById(R.id.c_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        getData();
    }


    private class CAdapter extends RecyclerView.Adapter<CHolder> {
        @Override
        public CHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_complain, parent, false);
            return new CHolder(inflate);
        }

        @Override
        public void onBindViewHolder(CHolder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClick();
                    notifyItemChanged(position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return lists.size();
        }
    }

    private void itemClick() {
        selectList.add("");
    }

    private class CHolder extends RecyclerView.ViewHolder {
        public CHolder(View inflate) {
            super(inflate);
        }
    }

    private void getData() {
        apiService.getComplain() //请求与View周期同步
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                    }
                })
                .subscribe(new Consumer<BaseResponse<BaseListEntity>>() {
                    @Override
                    public void accept(BaseResponse<BaseListEntity> response) throws Exception {
                        if (response.getCode() == 0) {
                            recyclerView.setAdapter(new CAdapter());
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }, new Consumer<ResponseThrowable>() {
                    @Override
                    public void accept(ResponseThrowable throwable) throws Exception {

                        ToastUtils.showShort(throwable.message);
                        throwable.printStackTrace();
                    }
                });

    }

    private void complain() {
        apiService.complain("informerId", "beInformerId", "informTypeId", "informName")
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                    }
                })
                .subscribe(new Consumer<BaseResponse<BaseListEntity>>() {
                    @Override
                    public void accept(BaseResponse<BaseListEntity> response) throws Exception {
                        if (response.getCode() == 0) {
                            recyclerView.setAdapter(new CAdapter());
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }, new Consumer<ResponseThrowable>() {
                    @Override
                    public void accept(ResponseThrowable throwable) throws Exception {

                        ToastUtils.showShort(throwable.message);
                        throwable.printStackTrace();
                    }
                });
    }

}
